/*
 * Copyright (C) 2014 Francisco José Morero Peyrona. All Rights Reserved.
 *
 * This file is part of JStatViewer project: https://code.google.com/p/jstat-viewer/
 *
 * GNU Classpath is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the free
 * Software Foundation; either version 3, or (at your option) any later version.
 *
 * Tapas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Tapas; see the file COPYING.  If not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package jstatviewer.ui;

import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import jstatviewer.Main;

/**
 * Shows a modaless dialog with one tab per category that can be produced by
 * JStat tool.
 *
 * @author peyrona
 */
final class Leyends
{
    private static JDialog dialog = null;

    //----------------------------------------------------------------------------//

    static void show()
    {
        if( dialog != null )
        {
            dialog.toFront();
            return;
        }

        dialog = new JDialog( Main.getFrame(), "Leyends explained", false );
        dialog.setContentPane( new JTabbedPane() );
        dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        dialog.setAlwaysOnTop( true );
        dialog.addWindowListener( new WindowAdapter()
        {
            @Override
            public void windowClosed( WindowEvent e )
            {
                Leyends.dialog = null;
            }
        });

        _class();
        compiler();
        gc();
        gccapacity();
        gccause();
        gcnew();
        gcnewcapacity();
        gcold();
        gcoldcapacity();
        gcpermcapacity();
        gcutil();
        printcompilation();

        dialog.pack();
        dialog.setVisible( true );
    }

    //----------------------------------------------------------------------------//

    private static void addTab( String title, String text )
    {
        JTextArea txt  = new JTextArea( text );
                  txt.setBorder( new EmptyBorder( 3,5,3,5 ) );
                  txt.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );

         JTabbedPane tabbedPane = (JTabbedPane) dialog.getContentPane();
                     tabbedPane.add( title, txt );

    }

    private static void _class()
    {
        String text = "Loaded    Number of classes loaded.\n" +
                      "Bytes     Number of Kbytes loaded.\n" +
                      "Unloaded  Number of classes unloaded.\n" +
                      "Bytes     Number of Kbytes unloaded.\n" +
                      "Time      Time spent performing class load and unload operations.";

        addTab( "-class", text );
    }

    private static void compiler()
    {
        String text = "Compiled      Number of compilation tasks performed.\n" +
                      "Failed        Number of compilation tasks that failed.\n" +
                      "Invalid       Number of compilation tasks that were invalidated.\n" +
                      "Time          Time spent performing compilation tasks.\n" +
                      "FailedType    Compile type of the last failed compilation.\n" +
                      "FailedMethod  Class name and method for the last failed compilation.";

        addTab( "-compiler", text );
    }

    private static void gc()
    {
        String text = "S0C   Current survivor space 0 capacity (KB).\n" +
                      "S1C   Current survivor space 1 capacity (KB).\n" +
                      "S0U   Survivor space 0 utilization (KB).\n" +
                      "S1U   Survivor space 1 utilization (KB).\n" +
                      "EC    Current eden space capacity (KB).\n" +
                      "EU    Eden space utilization (KB).\n" +
                      "OC    Current old space capacity (KB).\n" +
                      "OU    Old space utilization (KB).\n" +
                      "PC    Current permanent space capacity (KB).\n" +
                      "PU    Permanent space utilization (KB).\n" +
                      "YGC   Number of young generation GC Events.\n" +
                      "YGCT  Young generation garbage collection time.\n" +
                      "FGC   Number of full GC events.\n" +
                      "FGCT  Full garbage collection time.\n" +
                      "GCT   Total garbage collection time.";

        addTab( "-gc", text );
    }

    private static void gccapacity()
    {
        String text =   "NGCMN  Minimum new generation capacity (KB).\n" +
                        "NGCMX  Maximum new generation capacity (KB).\n" +
                        "NGC    Current new generation capacity (KB).\n" +
                        "S0C    Current survivor space 0 capacity (KB).\n" +
                        "S1C    Current survivor space 1 capacity (KB).\n" +
                        "EC     Current eden space capacity (KB).\n" +
                        "OGCMN  Minimum old generation capacity (KB).\n" +
                        "OGCMX  Maximum old generation capacity (KB).\n" +
                        "OGC    Current old generation capacity (KB).\n" +
                        "OC     Current old space capacity (KB).\n" +
                        "PGCMN  Minimum permanent generation capacity (KB).\n" +
                        "PGCMX  Maximum Permanent generation capacity (KB).\n" +
                        "PGC    Current Permanent generation capacity (KB).\n" +
                        "PC     Current Permanent space capacity (KB).\n" +
                        "YGC    Number of Young generation GC Events.\n" +
                        "FGC    Number of Full GC Events.";

        addTab( "-gccapacity", text );
    }

    private static void gccause()
    {
        String text = "LGCC  Cause of last Garbage Collection.\n" +
                      "GCC   Cause of current Garbage Collection.";

        addTab( "-gccause", text );
    }

    private static void gcnew()
    {
        String text = "S0C   Current survivor space 0 capacity (KB).\n" +
                      "S1C   Current survivor space 1 capacity (KB).\n" +
                      "S0U   Survivor space 0 utilization (KB).\n" +
                      "S1U   Survivor space 1 utilization (KB).\n" +
                      "TT    Tenuring threshold.\n" +
                      "MTT   Maximum tenuring threshold.\n" +
                      "DSS   Desired survivor size (KB).\n" +
                      "EC    Current eden space capacity (KB).\n" +
                      "EU    Eden space utilization (KB).\n" +
                      "YGC   Number of young generation GC events.\n" +
                      "YGCT  Young generation garbage collection time.";

        addTab( "-gcnew", text );
    }

    private static void gcnewcapacity()
    {
        String text = "NGCMN    Minimum new generation capacity (KB). \n" +
                      "NGCMX    Maximum new generation capacity (KB).\n" +
                      "NGC      Current new generation capacity (KB).\n" +
                      "S0CMX    Maximum survivor space 0 capacity (KB).\n" +
                      "S0C      Current survivor space 0 capacity (KB).\n" +
                      "S1CMX    Maximum survivor space 1 capacity (KB).\n" +
                      "S1C      Current survivor space 1 capacity (KB).\n" +
                      "ECMX     Maximum eden space capacity (KB).\n" +
                      "EC       Current eden space capacity (KB).\n" +
                      "YGC      Number of young generation GC events.\n" +
                      "FGC      Number of Full GC Events.";

        addTab( "-gcnewcapacity", text );
    }

    private static void gcold()
    {
        String text = "PC	Current permanent space capacity (KB).\n" +
                      "PU	 Permanent space utilization (KB).\n" +
                      "OC	Current old space capacity (KB).\n" +
                      "OU	 old space utilization (KB).\n" +
                      "YGC	 Number of young generation GC events.\n" +
                      "FGC	 Number of full GC events.\n" +
                      "FGCT	 Full garbage collection time.\n" +
                      "GCT	 Total garbage collection time.";

        addTab( "-gcold", text );
    }

    private static void gcoldcapacity()
    {
        String text = "OGCMN  Minimum old generation capacity (KB).\n" +
                      "OGCMX  Maximum old generation capacity (KB).\n" +
                      "OGC    Current old generation capacity (KB).\n" +
                      "OC     Current old space capacity (KB).\n" +
                      "YGC    Number of young generation GC events.\n" +
                      "FGC    Number of full GC events.\n" +
                      "FGCT   Full garbage collection time.\n" +
                      "GCT    Total garbage collection time.";

        addTab( "-gcoldcapacity", text );
    }


    private static void gcpermcapacity()
    {
        String text = "PGCMN  Minimum permanent generation capacity (KB).\n" +
                      "PGCMX  Maximum permanent generation capacity (KB).\n" +
                      "PGC    Current permanent generation capacity (KB).\n" +
                      "PC     Current permanent space capacity (KB).\n" +
                      "YGC    Number of young generation GC events.\n" +
                      "FGC    Number of full GC events.\n" +
                      "FGCT   Full garbage collection time.\n" +
                      "GCT    Total garbage collection time.";

        addTab( "-gcpermcapacity", text );
    }


    private static void gcutil()
    {
        String text = "S0    Survivor space 0 utilization as a percentage of the space's current capacity.\n" +
                      "S1    Survivor space 1 utilization as a percentage of the space's current capacity.\n" +
                      "E     Eden space utilization as a percentage of the space's current capacity.\n" +
                      "O     Old space utilization as a percentage of the space's current capacity.\n" +
                      "P     Permanent space utilization as a percentage of the space's current capacity.\n" +
                      "YGC   Number of young generation GC events.\n" +
                      "YGCT  Young generation garbage collection time.\n" +
                      "FGC   Number of full GC events.\n" +
                      "FGCT  Full garbage collection time.\n" +
                      "GCT   Total garbage collection time.";

        addTab( "-gcutil", text );
    }


    private static void printcompilation()
    {
        String text = "Compiled  Number of compilation tasks performed.\n" +
                      "Size      Number of bytes of bytecode for the method.\n" +
                      "Type      Compilation type.\n" +
                      "Method    Class and method names identifying the compiled method.";

        addTab( "-printcompilation", text );
    }
}